package com.angel.controller;

import com.angel.bean.Employee;
import com.angel.bean.InMessage;
import com.angel.bean.dto.InMessageDto;
import com.angel.service.InMessageService;
import com.angel.utils.AdminUserUtils;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/message")
public class MessageController {

    @Autowired
    private InMessageService inMessageService;

    /***
     * 发送出的列表
     * @param pn
     * @param model
     * @return
     */

    @RequestMapping("/send")
    public String getSendMessage(@RequestParam(value = "pn", defaultValue = "1") String pn, Model model) {
        int pageNo = 1;
        try {
            pageNo = Integer.valueOf(pn);
        } catch (Exception e) {

        }
        PageHelper.startPage(pageNo, 10);
        Employee employee = AdminUserUtils.getPrincipal().getEmployee();
        InMessage inMessage = new InMessage();
        inMessage.setFrom(employee.getEmId());
        List<InMessageDto> list = inMessageService.findMenssage(inMessage);
        if (list != null) {
            PageInfo pageInfo = new PageInfo(list);
            model.addAttribute("pageInfo", pageInfo);
        }
        return "sendmessageManager";
    }

    /**
     * 收到的消息列表
     *
     * @param pn
     * @param model
     * @return
     */
    @RequestMapping("/ref")
    public String getrefMessage(@RequestParam(value = "pn", defaultValue = "1") String pn, Model model) {
        int pageNo = 1;
        try {
            pageNo = Integer.valueOf(pn);
        } catch (Exception e) {

        }
        PageHelper.startPage(pageNo, 10);
        Employee employee = AdminUserUtils.getPrincipal().getEmployee();
        InMessage inMessage = new InMessage();
        inMessage.setTo(employee.getEmId());
        inMessage.setStatus(0);
        List<InMessageDto> list = inMessageService.findMenssage(inMessage);
        if (list != null) {
            PageInfo pageInfo = new PageInfo(list);
            model.addAttribute("pageInfo", pageInfo);
        }
        return "messageManager";
    }


    @RequestMapping("up")
    @ResponseBody
    public Map upMsg(@RequestParam("id") String id) {
        Map result = new HashMap();
        result.put("success", false);
        Integer tid = 0;
        try {
            tid = Integer.valueOf(id);
        } catch (Exception e) {
            tid = 0;
        }
        InMessage inMessage = new InMessage();
        inMessage.setStatus(1);
        inMessage.setId(tid);
        int re = inMessageService.update(inMessage);
        if (re != 0) {
            result.put("success", true);
            result.put("msg", "删除成功");
        } else {
            result.put("msg", "删除失败");
        }

        return result;
    }


}
